package com.vector.demo.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 拦截器
 */
public class LoginInterceptor implements HandlerInterceptor {
    @Override //进入Controller之前执行该方法
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //登录拦截的业务逻辑
        System.out.println("-------登录请求拦截器--------------");
        System.out.println(request.getRequestURI().toString());
        if(request.getSession().getAttribute("admin")!=null||request.getSession().getAttribute("student")!=null||request.getSession().getAttribute("teacher")!=null) {
            return true;
        }else {
            System.out.println("请先登录");
            request.setAttribute("msg","请先登录");
            //请求转发调到 登录页面
            response.sendRedirect("/login");
            return false;
        }
    }


    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    }
}